#include "threadPool.hpp"
#include <random>
#include <signal.h>

static bool exit_stop=false;

int add(int num1,int num2)
{
    std::this_thread::sleep_for(std::chrono::seconds(5));
    return num1+num2;
}

void sighandler(int sig)
{
    exit_stop=true;
}

int main()
{
    //1.注册信号
    signal(SIGINT,sighandler);

    ThreadPool::getInstance().start();
    std::random_device rd;
    std::mt19937 generator(rd());
    std::uniform_int_distribution<int> distribution(0,255);
    std::vector<std::pair<std::string,std::future<int>>> v;//保存结果
    for(int i=0;i<10;i++)
    {
        int num1=distribution(generator);
        int num2=distribution(generator);
        std::string str=std::to_string(num1)+"+"+std::to_string(num2);
        auto res=ThreadPool::getInstance().addTask(add,num1,num2);
        v.emplace_back(std::make_pair(str,std::move(res)));
    }

    std::this_thread::sleep_for(std::chrono::seconds(30));
    for(auto& iter:v)
    {
        std::cout<<iter.first<<"="<<iter.second.get()<<std::endl;
    }
    while(!exit_stop)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    
    return 0;
}